package org.onvivals.admin.module.ums.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.onvivals.common.param.PageParam;
import org.onvivals.common.web.PageResult;
import org.onvivals.common.web.Result;
import org.onvivals.module.ums.entity.UmsNotice;
import org.onvivals.module.ums.param.UmsNoticeParam;
import org.onvivals.module.ums.service.IUmsNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/ums/notice")
public class UmsNoticeController {

	private final IUmsNoticeService pmsNoticeService;

	@Operation(summary = "分页查询全部通知公告")
	@GetMapping("/page")
	public Result<PageResult<UmsNotice>> page(UmsNoticeParam param) {
		PageParam<UmsNotice, UmsNoticeParam> page = new PageParam<>(param);
		page.setDefaultOrder("create_time desc");
		IPage<UmsNotice> pa = pmsNoticeService.page(page, page.getWrapper());
		PageResult<UmsNotice> pageResult = new PageResult<>(pa.getRecords(), pa.getTotal());
		return Result.OK("查询成功", pageResult);
	}

	@Operation(summary = "查询全部通知公告")
	@GetMapping()
	public Result<List<UmsNotice>> list(UmsNoticeParam param) {
		PageParam<UmsNotice, UmsNoticeParam> page = new PageParam<>(param);
		List<UmsNotice> pmsLlmConfigList = pmsNoticeService.list(page.getOrderWrapper());
		return Result.OK("查询成功", pmsLlmConfigList);
	}

	@Operation(summary = "根据id查询通知公告")
	@GetMapping("/{id}")
	public Result<UmsNotice> get(@PathVariable("id") Long id) {
		UmsNotice pmsNotice = pmsNoticeService.getById(id);
		return Result.OK("查询成功", pmsNotice);
	}

	@Operation(summary = "添加通知公告")
	@PostMapping()
	public Result<?> save(@RequestBody UmsNotice umsNotice) {
		if (pmsNoticeService.save(umsNotice)) {
			return Result.OK("添加成功");
		}
		return Result.error("添加失败");
	}

	@Operation(summary = "修改通知公告")
	@PutMapping()
	public Result<?> update(@RequestBody UmsNotice umsNotice) {
		if (pmsNoticeService.updateById(umsNotice)) {
			return Result.OK("修改成功");
		}
		return Result.error("修改失败");
	}

	@Operation(summary = "删除通知公告")
	@DeleteMapping("/{id}")
	public Result<?> remove(@PathVariable("id") Long id) {
		if (pmsNoticeService.removeById(id)) {
			return Result.OK("删除成功");
		}
		return Result.error("删除失败");
	}
}
